package bi.example.demo9;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(value="/ScoreAddServlet")
public class ScoreAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stu_id = req.getParameter("txtPname1");
        String cou_id = req.getParameter("txtPname2");
        String reg_grade = req.getParameter("txtPname3");
        String score = req.getParameter("txtPname4");

        String url = "jdbc:mysql://localhost:3306/studentdb?serverTimezone=UTC";
        String db_name = "root";
        String db_pass = "123456";
        String sql = "insert into score(stu_id,course_id,regular_grade,score)values (?,?,?,?)";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, db_name, db_pass);
            PreparedStatement prep = conn.prepareStatement(sql);
            prep.setString(1, stu_id);
            prep.setString(2, cou_id);
            prep.setString(3, reg_grade);
            prep.setString(4, score);
            int i = prep.executeUpdate();

            if (i > 0) {
                resp.sendRedirect("index_1_EnterStudentGrades.jsp");
            }else{
                resp.sendRedirect("index.jsp");
            }

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

